home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
The World of Computer Software.iso
/
pbaseiv.zip
/
P4DOS010.TIP
< prev
next >
Wrap
Text File
|
1991-12-16
|
4KB
|
118 lines
[This tip contains an example of a technique that looked
good on the surface, but actually turned out to have a
serious flaw. Read on, and see if you can figure out what it
is before you get to the end.]
It's frustrating to deal with uppercase and lowercase
arguments passed to batch files--`PROG file', `PROG FILE',
or even `ProG FiLe', for example. Testing for all
combinations of case (making the batch program case
insensitive) usually requires a series of messy IF
statements or complex FOR loops.
But I've hit upon an approach that lets batch files convert
arguments to uppercase before using them. The technique
relies on a side effect of the DOS PATH command, which,
automatically converts its parameter to uppercase before
storing it in the environment variable. By saving the
current PATH, using it to process parameters, and then
restoring the PATH, this trick makes a handy case converter
in any batch file.
A sample batch file, RUN.BAT (see listing), converts
arguments to uppercase, then runs a program. First, it
checks that you've entered at least two items: a program to
run, and any arguments for that program. After this, it
saves PATH in OLDPATH and sets RUN to the target program's
name. A loop then uses SHIFT to build a string in PATH out
of each argument. This converts the arguments to uppercase.
After the loop, PATH is set to OLDPATH, which is erased to
conserve environment space. The %RUN% command then runs the
program with the converted arguments.
William T. McGowan III
Columbia, South Carolina
Editor's note: For a sample of how you can use this clever
idea, run TESTPROG.BAT (see second listing below). The batch
file simulates a program that recognizes two fictitious
commands, OPEN and CLOSE.
When you enter testprog OPEN or testprog CLOSE, TESTPROG
confirms that it received those commands. If you enter those
same commands in lowercase, the program displays an error
message. But when you use Mr. McGowan's batch file to run
TESTPROG, you can type arguments using any combination of
lowercase and uppercase letters.
If you get an `Out of environment space' message, add the
line shell command.com /e:384 /p to the end of your
CONFIG.SYS file. When you reboot, DOS will set aside 384
bytes (instead of the default 256) for environment
variables. If this is still not enough, try 512 or more. The
`/p' makes the change to COMMAND.COM permanent.
Postscript: This tip, which appeared in the January 1990
issue of PC World, looks good on the surface, but it carries
with it a serious potential pitfall. Can you see what it is?
Answer: the user's path will be destroyed if he breaks out
of the batch while the path holds a string that's being
case-converted. What's worse, the sample batch file spends a
lot of time in a loop, during which the PATH isn't set to
anything valid.
Luckily, there's a better and simpler way to make a batch
file ignore case. The GOTO command, when used with an
environment variable or a command line parameter (e.g. GOTO
%1) will convert its parameter to uppercase before jumping.
This is much safer, because it can never destroy your path
and eliminates the need to test every possible spelling of
every parameter.
RUN.BAT demonstrates a not-too-safe way to use PATH: as an
uppercase converter in batch files. To avoid leaving a user
with an invalid path, we recommend using the GOTO command
on a parameter or an environment variable in situations
where case needs to be ignored.
@echo off
if "%2"=="" goto HELP
set OLDPATH=%PATH%
set RUN=%1
:NEXT
shift
PATH=%1
set RUN=%RUN% %PATH%
if not "%2"=="" goto NEXT
set PATH=%OLDPATH%
set OLDPATH=
%RUN%
:HELP
echo Enter run x arg1 arg2...
echo where x is a program and
echo args are option arguments
TESTPROG.BAT: Original test program for RUN.BAT
@echo off
if "%1"=="" goto ERROR
if %1==OPEN goto OPEN
if %1==CLOSE goto CLOSE
goto ERROR
:OPEN
echo You selected OPEN
goto END
:CLOSE
echo You selected CLOSE
goto END
:ERROR
echo Bad or missing command
:END
Title: Insensitive Batch Arguments
Category: DOS
Issue date: Jan 1991
Editor: Tom Swan
Supplementary files: NONE